package xyz.cerberu.cet.global.utils;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author β世界
 * Created on 23:25
 * @Description 时间计算工具
 */
public class TimeUtils {
    /** 星期队列，因为Calendar获取的星期周日是第一天 **/
    private static final int[] WEEKS = {0,1,2,3,4,5,6};


    /**
     * @author β世界 on 23:40 2020/4/10
     * @Description 计算季度(传递一个Calendar对象，我们从中获取到时间)
     * @param mouth: 当前月
     * @return java.lang.String
     **/
    public static String calculateQuarter(int mouth){
        if(mouth >= 1 && mouth <= 3){
            return ("q1");
        }else if(mouth >= 4 && mouth <= 6){
            return ("q2");
        }else if(mouth >= 7 && mouth <= 9){
            return ("q3");
        }else if(mouth >= 10 && mouth <= 12){
            return ("q4");
        }
        return null;
    }

    /**
     * @author β世界 on 23:31 2020/4/11
     * @Description 计算一个时间的周期
     **/
    public static int calculateWeek(Calendar date){
        //一周的第几天
        int w = (date.get(Calendar.DAY_OF_WEEK)-1);
        if (w < 0) {
            w = 0;
        }
        return WEEKS[w];
    }

    public static Date stringConvertDate(String date, String format){
        Date result = null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            result = new Date(formatter.parse(date).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String dateConvertString(Date date, String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

}
